#!/usr/bin/env python
#coding: utf-8
'''
Created on 2017年3月8日

@author: Administrator
'''
from common.decorators import login_required
from django.db.models.query_utils import Q
from django.http.response import HttpResponse
from django.template.response import TemplateResponse
from funcresource import settings
from funcresource.decorators import user_funcresource
from funcresource.models import UserFunctional
import copy
import ujson as json


@login_required
@user_funcresource
def index(request, template="funcresource/admin/userfunctional/userfunctional.html"):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    return TemplateResponse(request, template, rsp_data)
    
@login_required
def list(request):
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    rsp_data["data"] = []
    start = int(request.POST['start'])
    length = int(request.POST['length'])
    keyword = request.POST["search[value]"]
    state = request.POST["columns[4][search][value]"]
    state = '' if state == '' else state[1:-1]
    draw = request.POST['draw']
    q = Q()
    if keyword:
        q.add(Q(**{"user__username__icontains": keyword}), Q.OR)
        q.add(Q(**{"func__name__icontains": keyword}), Q.OR)
    if state:
        q.add(Q(**{"state": state}), Q.AND)
    count = UserFunctional.objects.filter(q).count()
    userfunctions = UserFunctional.objects.filter(q)[start:start + length]
    for userfunction in userfunctions:
        record = {
            "id":userfunction.id,
            "username":userfunction.user.username,
            'state':userfunction.state,
            'state_desc':settings.get_state_desc(userfunction.state),
            "func_name":userfunction.func.name,
            "func_resource_name":userfunction.func.func_resource.name,
        }
        rsp_data["data"].append(record)
    rsp_data["recordsTotal"] = count
    rsp_data["recordsFiltered"] = count
    rsp_data["draw"] = draw
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')

@login_required
def delete(request):
    """
        func:
                                删除
        parameters:
            userfunctional_ids:id集合
    """
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    #获取并赋值
    userfunctional_ids = request.POST["userfunctional_ids"]
    if userfunctional_ids:
        userfunctional_ids = userfunctional_ids.split(",")
    UserFunctional.objects.filter(id__in=userfunctional_ids, state=settings.StateEnum.VALID).update(state=settings.StateEnum.DELETED)
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')

@login_required
def reopen(request):
    """
        func:
                                重新开通
        parameters:
            userfunctional_ids:id集合
    """
    rsp_data = copy.copy(settings.ERROR["SUCC"])
    #获取并赋值
    userfunctional_ids = request.POST["userfunctional_ids"]
    if userfunctional_ids:
        userfunctional_ids = userfunctional_ids.split(",")
    userfunctionals = UserFunctional.objects.filter(id__in=userfunctional_ids, state=settings.StateEnum.DELETED).exclude(func__state=settings.StateEnum.VALID)
    if userfunctionals:
        for userfunctional in userfunctionals:
            rsp_data = copy.copy(settings.ERROR["STATE_ERR"])
            rsp_data["msg"] = u"重新开通失败，请重新开通功能【%s】" % (userfunctional.func.name)
            return HttpResponse(json.dumps(rsp_data), mimetype='application/json')
            
    UserFunctional.objects.filter(id__in=userfunctional_ids, state=settings.StateEnum.DELETED).update(state=settings.StateEnum.VALID)
    return HttpResponse(json.dumps(rsp_data), mimetype='application/json')
